package com.taxi.driver.module.account.modify;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.qianxx.utils.text.RegUtils;
import com.taxi.driver.BuildConfig;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.module.account.modify.dagger.DaggerPwdModifyComponent;
import com.taxi.driver.module.account.modify.dagger.PwdModifyModule;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.util.SpaceFilter;
import com.taxi.driver.widget.dialog.DialogCreator;
import com.taxi.driver.widget.dialog.OnDefaultListener;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：修改密码
 */
public class PwdModifyActivity extends BaseActivity implements PwdModifyContract.View, TextWatcher {

    @BindView(R.id.et_old_pw)
    EditText mEtOldPw;
    @BindView(R.id.et_new_pw)
    EditText mEtNewPw;
    @BindView(R.id.et_comfig_pw)
    EditText mEtComfigPw;
    @BindView(R.id.btn_confirm_change)
    Button btn_confirm_change;

    @Inject
    PwdModifyPresenter mPresenter;
    Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //该属性能防止屏幕被截图和录制。这个属性就是可以防止当前Activity不会被录制
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE);
        setContentView(R.layout.activity_pwd_modify);
        ButterKnife.bind(this);

        DaggerPwdModifyComponent.builder()
                .appComponent(getAppComponent())
                .pwdModifyModule(new PwdModifyModule(this))
                .build().inject(this);
        mEtOldPw.addTextChangedListener(this);
        mEtNewPw.addTextChangedListener(this);
        mEtComfigPw.addTextChangedListener(this);
        mEtNewPw.setFilters(new InputFilter[]{new SpaceFilter(this)});
        //更改hint字体大小
            SpannableString hintString = new SpannableString(getString(R.string.hint4));
            AbsoluteSizeSpan hintSize = new AbsoluteSizeSpan(16, true);
            hintString.setSpan(hintSize, 0, hintString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mEtNewPw.setHint(hintString);

    }

    @OnClick({R.id.btn_confirm_change})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm_change:
                String oldPw = mEtOldPw.getText().toString().trim();
                if (TextUtils.isEmpty(oldPw)) {
                    toast(getResources().getString(R.string.please_input_old_pw));
                    return;
                }
                String newPw = mEtNewPw.getText().toString().trim();
                if (TextUtils.isEmpty(newPw)) {
                    toast(getResources().getString(R.string.please_input_new_pw));
                    return;
                }
                    if (!RegUtils.isStringFormatCorrectNew(newPw)) {
                        toast(getResources().getString(R.string.hint4));
                        return;
                    }

                String comfirmPw = mEtComfigPw.getText().toString().trim();
                if (!comfirmPw.equals(newPw)) {
                    toast(getResources().getString(R.string.new_cofirm_different));
                    return;
                }
                mPresenter.resetPw(newPw, oldPw);
                break;
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void resetSucc() {
        showDialog();
    }

    @Override
    public void resetFail(int errorCode, String errorMsg) {
        toast(errorMsg);
    }

    @Override
    public void logoutSuccess() {
        LoginActivity.start(this);

        finish();
    }

    private void showDialog() {
        dialog = DialogCreator.defaultDialog(this, getResources().getString(R.string.relogin), false, new OnDefaultListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onComfig() {
                mPresenter.reqLogout();
                if (dialog != null) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
        });
        dialog.show();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(mEtOldPw.getText().toString())
                && !TextUtils.isEmpty(mEtNewPw.getText().toString())
                && !TextUtils.isEmpty(mEtComfigPw.getText().toString())) {
            btn_confirm_change.setEnabled(true);
            btn_confirm_change.setBackgroundResource(R.drawable.sel_dark_btn);
        } else {
            btn_confirm_change.setEnabled(false);
            btn_confirm_change.setBackgroundResource(R.drawable.dra_dark_btn_cant_press);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
